package cn.itsource.basic.controller;

import cn.itsource.basic.dto.LoginDto;
import cn.itsource.basic.exception.BusinessException;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.user.service.ILogininfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

//用户登录接口
@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ILogininfoService iLogininfoService;

    //管理员账号登陆接口
    @PostMapping("/account")
    public AjaxResult accountLogin(@RequestBody LoginDto loginDto){
        try {
            //在controller层返回则个map，也可以像课件在service层返回
            Map<String,Object> map = iLogininfoService.accountLogin(loginDto);
            return AjaxResult.me().setResultObj(map);
        }catch (BusinessException e){
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，稍后重试");
        }

    }

    //用户账号登陆接口
    @PostMapping("/useraccount")
    public AjaxResult useraccountLogin(@RequestBody LoginDto loginDto){
        try {
            //在controller层返回则个map，也可以像课件在service层返回
            Map<String,Object> map = iLogininfoService.accountLogin(loginDto);
            return AjaxResult.me().setResultObj(map);
        }catch (BusinessException e){
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，稍后重试");
        }

    }

    /**
     * 微信登录接口
     * @param code
     * @return
     */
    @GetMapping("/wechat/{code}")
    public AjaxResult wechatLogin(@PathVariable("code") String code){
        try {
            return iLogininfoService.wechatLogin(code);
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统异常，请稍后重试!!!");
        }
    }

    /**
     * 微信绑定接口
     *  定义一个dto或者map都可以 区别在于dto需要事先定义
     */
    @PostMapping("/wechat/binder")
    public AjaxResult wechatBinder(@RequestBody Map<String,String> map){
        try {
            return iLogininfoService.wechatBinder(map);
        }catch (BusinessException e){
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，稍后重试");
        }

    }

}
